<template>
  <div class="cart-bar">
    <div class="count">
      <check-btn @click.native="selectAll" :is-checked="checkAll"></check-btn>
      <span class="select-all">全选</span>
      <span>合计：￥ {{ totalPrice | priceFilter}}</span>
    </div>
    <div class="buy">去结算({{ totalCount }})</div>
  </div>
</template>

<script>
import checkBtn from "@/components/content/checkBtn"

// vuex相关
import {CHECKED_ALL_GOODS} from "@/store/mutations-type";
import {mapGetters} from 'vuex'

export default {
  name: "CartBottomBar",
  data() {
    return {
    }
  },
  components:{
    checkBtn
  },
  methods:{
    selectAll(){
      this.$store.commit(CHECKED_ALL_GOODS,this.checkAll)
    }
  },
  computed:{
    ...mapGetters(['cartLength','cartList','checkedGoods']),
    totalPrice() {
      return this.$store.getters.checkedGoods.reduce((preValue,item) =>{
        return preValue+ item.price * item.count
      },0)
    },
    totalCount() {
      return this.$store.getters.checkedGoods.reduce((preValue,item) =>{
        return preValue +  item.count
      },0)
    },
    checkAll() {
      if(this.cartLength === 0) return false
      return !this.cartList.find(item => !item.checked)
    }
  },
  filters:{
    priceFilter(value) {
      return value.toFixed(2)
    }
  }
}
</script>

<style scoped>
.cart-bar{
  position: fixed;
  left: 0;
  right: 0;
  bottom: 49px;
  display: flex;
  height: 44px;
  background-color: #EEEEEE;
  z-index: 9;
  box-shadow: 0 -3px 5px rgba(0,0,0,.2);
}
.cart-bar .buy{
  width: 100px;
  background-color: #FF4500;
  text-align: center;
  line-height: 44px;
  color: #fff;
}
.cart-bar .count{
  flex: 1;
  display: flex;
  align-items: center;
  padding-left: 10px ;
}
.cart-bar .count .select-all{
  margin: 0 15px 0 5px;
  font-size: 13px;
}
</style>